using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/* OWED */
using OWED.Document;
using OWED.Network;
using OWED.Network.Messages;
using OWED.Network.Messages.Reply;
using OWED.Network.Messages.General;

public partial class ViewProposal : System.Web.UI.Page {

	/// <summary>
	/// Authentication details
	/// </summary>
	String[] AuthDetails;

	/// <summary>
	/// Backend connection
	/// </summary>
	Connection conn;

	/// <summary>
	/// List of proposals
	/// </summary>
	ListProposals request;

	/// <summary>
	/// Reply of proposals
	/// </summary>
	ProposalListReply reply;

	/// <summary>
	/// ErrorMessage for View proposal
	/// </summary>
	protected Literal pageOutput;

	protected void Page_Load(object sender, EventArgs e)
	{
		pageOutput.Text = "";
		/* check if user is logged in */
		if (!IsLoggedIn.Check(Request.Cookies["owed"])) {
			Response.Redirect("Login.aspx");
		} else {
			AuthDetails = IsLoggedIn.GetAuthentication(Request.Cookies["owed"]);
		}

		if (Request.QueryString["document"] == null) {
			pageOutput.Text = "<h2>Error</h2>No document name supplied";
			return;
		}

		/* instantiate connection and login*/
		conn = null;
		WebLogin.C(out conn, AuthDetails[0], AuthDetails[1]);
		
		/* send proposal request */
		request = new ListProposals(new User(AuthDetails[0]), new DocName(Request.QueryString["document"]));
		reply = (ProposalListReply)MessageGet.GetFromXml(conn.Send(request.ToXml()));
		if (reply == null) {
			pageOutput.Text = "<h2>Latest revision</h2>No proposals have been made";
			return;
		}

		foreach (Proposal p in reply.Proposals)
		{
			pageOutput.Text += "<input type=\"radio\" name=\"whichProposal\" value=\"" + p.ProposalID.number + "\"/>";
			//foreach (Change c in p.Changes.ChangeAtoms)
				//pageOutput.Text += c.Text + "<br/>";
		}
		pageOutput.Text += "<input type=\"hidden\" name=\"whichDocument\" value=\"" + Request.QueryString["document"] + "\"/>";
	}
}
